// 中间件

import Koa from 'koa'
import koaBody from "koa-body"
import path from 'path'
import AuthMiddleware from './middleware/AuthMiddleware'
import Cors from './middleware/Cors'
import AccessLogMiddleware from './middleware/AccessLogMiddleware'

export const useMiddlewares = <T extends Koa>(app: T): T => {
    app.use(Cors)
    // app.use(AuthMiddleware)
    app.use(AccessLogMiddleware)
    app.use(
        koaBody({// 相当于http请求参数的解析，包括二进制，可以处理文件上传
            multipart: true,
            formidable: {
                uploadDir: path.join(__dirname, "../upload/"), // 设置上传目录
                keepExtensions: true,      // 保持文件后缀
                maxFieldsSize: 800 * 1024 * 1024, //文件大小
            }
        })
    )

    return app;
}
